﻿using MyConvert;
using MyDateTime;
using MySQLite.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MySQLite.DataHelp
{
    public class LogHelper
    {
        #region 对外接口

        #region 本地日志

        /// <summary>
        /// 添加本地日志信息
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool AddLocalLogInfo(string content)
        {
            try
            {
                LogModel logModel = GetLogModel("本地", "信息", content);
                bool flag = InsertLogInfo(logModel);
                return flag;
            }
            catch (Exception)
            {
            }
            return false;
        }

        /// <summary>
        /// 添加本地日志警告
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool AddLocalLogWarn(string content)
        {
            try
            {
                LogModel logModel = GetLogModel("本地", "警告", content);
                bool flag = InsertLogInfo(logModel);
                return flag;
            }
            catch (Exception)
            {
            }
            return false;
        }

        /// <summary>
        /// 添加本地日志错误
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool AddLocalLogError(string content)
        {
            try
            {
                LogModel logModel = GetLogModel("本地", "错误", content);
                bool flag = InsertLogInfo(logModel);
                return flag;
            }
            catch (Exception)
            {
            }
            return false;
        }

        #endregion 本地日志

        #region 联网日志

        /// <summary>
        /// 添加联网日志信息
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool AddNetworkLogInfo(string content)
        {
            try
            {
                LogModel logModel = GetLogModel("联网", "信息", content);
                bool flag = InsertLogInfo(logModel);
                return flag;
            }
            catch (Exception)
            {
            }
            return false;
        }

        /// <summary>
        /// 添加联网日志警告
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool AddNetworkLogWarn(string content)
        {
            try
            {
                LogModel logModel = GetLogModel("联网", "警告", content);
                bool flag = InsertLogInfo(logModel);
                return flag;
            }
            catch (Exception)
            {
            }
            return false;
        }

        /// <summary>
        /// 添加联网日志错误
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool AddNetworkLogError(string content)
        {
            try
            {
                LogModel logModel = GetLogModel("联网", "错误", content);
                bool flag = InsertLogInfo(logModel);
                return flag;
            }
            catch (Exception)
            {
            }
            return false;
        }

        #endregion 联网日志

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logType"></param>
        /// <param name="logLevel"></param>
        /// <param name="logContent"></param>
        /// <returns></returns>
        public static LogModel GetLogModel(string? logType, string? logLevel, string logContent)
        {
            var logModel = new LogModel()
            {
                Guid = Guid.NewGuid().ToString("N"),
                LogType = logType,
                LogLevel = logLevel,
                LogContent = logContent,
                CreateTime = MyDateTimeHelper.CurrentSecondDateTime,
                CreateUserGuid = "admin",
                CreateUserNo = "admin",
                CreateUserName = "admin",
                ModifyTime = MyDateTimeHelper.CurrentSecondDateTime,
                ModifyUserGuid = "admin",
                ModifyUserNo = "admin",
                ModifyUserName = "admin"
            };

            return logModel;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logModel"></param>
        /// <returns></returns>
        public static bool InsertLogInfo(LogModel logModel)
        {
            string sql = $" insert into T_Log(Guid,LogType,LogLevel,LogContent,CreateTime,CreateUserGuid,CreateUserNo,CreateUserName,ModifyTime,ModifyUserGuid,ModifyUserNo,ModifyUserName,Remark) values('{logModel.Guid}','{logModel.LogType}','{logModel.LogLevel}','{logModel.LogContent}','{logModel.CreateTime}','{logModel.CreateUserGuid}','{logModel.CreateUserNo}','{logModel.CreateUserName}','{logModel.ModifyTime}','{logModel.ModifyUserGuid}','{logModel.ModifyUserNo}','{logModel.ModifyUserName}','{logModel.Remark}') ";
            bool flag = MySQLiteHelper.Instance.ExecuteSql(sql);
            return flag;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public static Pages<LogModel> GetLogInfos(Page<LogCondition> page)
        {
            Pages<LogModel> results = new Pages<LogModel>();
            string strWhere = " where 1=1 ";
            if (!string.IsNullOrEmpty(page.PageModel?.LogType))
            {
                strWhere += $" and LogType ='{page.PageModel?.LogType}' ";
            }
            if (!string.IsNullOrEmpty(page.PageModel?.LogLevel))
            {
                strWhere += $" and LogLevel ='{page.PageModel?.LogLevel}' ";
            }
            if (!string.IsNullOrEmpty(page.PageModel?.LogContent))
            {
                strWhere += $" and LogContent like '%{page.PageModel?.LogContent}%' ";
            }
            if (!string.IsNullOrEmpty(page.PageModel?.StartTime))
            {
                strWhere += $" and ModifyTime >='{Convert.ToDateTime(page.PageModel?.StartTime).ToString("yyyy-MM-dd HH:mm:ss")}' ";
            }
            if (!string.IsNullOrEmpty(page.PageModel?.EndTime))
            {
                strWhere += $" and ModifyTime <='{Convert.ToDateTime(page.PageModel?.EndTime).ToString("yyyy-MM-dd HH:mm:ss")}' ";
            }
            results.PageNo = page.PageNo;
            results.PageRow = page.PageRow;
            results.PageFlag = page.PageFlag;
            results.TotalCount = GetCount(strWhere);
            results.SetPageState();
            results.PageModels = GetModels(page.PageFlag, page.PageNo, page.PageRow, strWhere);
            return results;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool DeleteLogs()
        {
            string sql = $" DELETE from T_Log where strftime('%s', 'now') - strftime('%s', CreateTime) > 10 * 24 * 60 * 60 ";
            bool flag = MySQLiteHelper.Instance.ExecuteSql(sql);
            return flag;
        }

        #endregion 对外接口

        #region 内部使用

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageFlag"></param>
        /// <param name="pageNo"></param>
        /// <param name="pageRow"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        static List<LogModel> GetModels(bool pageFlag, int pageNo, int pageRow, string strWhere)
        {
            List<LogModel> results = new List<LogModel>();
            string sql = $" select T_Log.Guid,cType.ConfigName as LogType,cLevel.ConfigName as LogLevel,LogContent,CreateTime,CreateUserGuid,CreateUserNo,CreateUserName,ModifyTime,ModifyUserGuid,ModifyUserNo,ModifyUserName,Remark from T_Log left join T_Config cType on LogType = cType.Guid left join T_Config cLevel on LogLevel = cLevel.Guid {strWhere} order by ModifyTime desc ";
            if (pageFlag)
            {
                var begin = (pageNo - 1) * pageRow;
                sql += $" limit {begin},{pageRow} ";
            }
            DataTable dt = MySQLiteHelper.Instance.ExecuteSqlForDataTable(sql);
            results = ModelConvert.ConvertToModels<LogModel>(dt);
            return results;
        }

        /// <summary>
        /// 获取查询条件下的用户总数量
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        static int GetCount(string strWhere)
        {
            string sql = $" select count(Guid) from T_Log {strWhere} ";
            var count = MySQLiteHelper.Instance.ExecuteSqlToInt(sql);
            return count;
        }

        #endregion 内部使用
    }

}
